package com.aaa.javaweb.dao.impl;

import com.aaa.javaweb.dao.TPandianDao;
import com.aaa.javaweb.entity.SheShi;
import com.aaa.javaweb.entity.TChuku;
import com.aaa.javaweb.entity.TPandian;
import com.aaa.javaweb.entity.TPandiandetail;
import com.aaa.javaweb.until.JDBCUntil;
import com.aaa.javaweb.until.TransBean;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class TPandianDaoImpl implements TPandianDao {
    @Override
    public int addPandian(Map map) {
        String sql = "insert into t_pandian(pddate,pdrid,pdrid2) values (now(),?,?)";
        return JDBCUntil.Updatekeys(sql,map.get("pdrid"),map.get("pdrid2"));
    }

    @Override
    public int addPandianDetail(List<Map> tableData, int rukuid) {
        String sql = "insert into t_pandiandetail (id,pdid,ssid,sjcount,count,comm) values (null,?,?,?,?,?)";
        if (tableData != null) {
            int i = 0;
            for (Map map : tableData) {
                i += JDBCUntil.Updateout(sql,rukuid, map.get("ssid"),map.get("sjcount"), map.get("count"),map.get("comm"));
            }
            return i;
        }
        return 0;
    }

    @Override
    public int pandianAndAddBookCount(List<Map> tableData) {
        String sql = "update t_sheshi set count = ? where id = ? ";
        if (tableData != null) {
            int i = 0;
            for (Map map : tableData) {
                i += JDBCUntil.Updateout(sql, map.get("sjcount"), map.get("ssid"));
            }
            return i;
        }
        return 0;
    }

    @Override
    public long getCount(TPandian chuku) {
        StringBuilder sql = new StringBuilder("select count(*) c from t_pandian r where 1=1");
        if (chuku.getPdrid()!=null&&chuku.getPdrid()!=""){
            sql.append(" and r.pdrid like '%").append(chuku.getPdrid()).append("%'");
        }
        if (chuku.getPdrid2()!=null&&chuku.getPdrid2()!=""){
            sql.append(" and r.pdrid2 like '%").append(chuku.getPdrid2()).append("%'");
        }
        if (chuku.getCreatedTime()!=null&&chuku.getCreatedTime()!=""){
            sql.append(" and r.pddate >= '").append(chuku.getCreatedTime()).append("'");
        }
        if (chuku.getEndTime()!=null&&chuku.getEndTime()!=""){
            sql.append(" and r.pddate <= '").append(chuku.getEndTime()).append("'");
        }
        sql.append(" order by r.id desc ");
        List<Map> maps = JDBCUntil.selectMap(sql + "");
        if (maps.size()>0){
            return (long) maps.get(0).get("c");
        }
        return 0;
    }

    @Override
    public List<TPandian> getAllRuku(TPandian chuku) {
        StringBuilder sql = new StringBuilder("select * from t_pandian r where 1=1");
        if (chuku.getPdrid()!=null&&chuku.getPdrid()!=""){
            sql.append(" and r.pdrid like '%").append(chuku.getPdrid()).append("%'");
        }
        if (chuku.getPdrid2()!=null&&chuku.getPdrid2()!=""){
            sql.append(" and r.pdrid2 like '%").append(chuku.getPdrid2()).append("%'");
        }
        if (chuku.getCreatedTime()!=null&&chuku.getCreatedTime()!=""){
            sql.append(" and r.pddate >= '").append(chuku.getCreatedTime()).append("'");
        }
        if (chuku.getEndTime()!=null&&chuku.getEndTime()!=""){
            sql.append(" and r.pddate <= '").append(chuku.getEndTime()).append("'");
        }
        sql.append(" order by r.id desc limit ?,?");
        List<Map> maps = JDBCUntil.selectMap(sql + "", (chuku.getPage() - 1) * chuku.getLimit(), chuku.getLimit());
        return TransBean.populate(TPandian.class,maps);
    }

    @Override
    public List<Map> getAllRukuDetailByRukuId(String pdid) {
        String sql = "select r.*,b.name,b.gys from t_pandiandetail r join t_sheshi b on r.ssid = b.id where r.pdid = ?";
        return JDBCUntil.selectMap(sql, pdid);
    }

    @Override
    public long getSheShiCount() {
        String s = "select count(*) c from t_sheshi r where typeid=1";
        List<Map> maps = JDBCUntil.selectMap(s);
        if (maps.size()>0){
            return (long) maps.get(0).get("c");
        }
        return 0;
    }

    @Override
    public List<TPandiandetail> getAllSheShi(TPandian pandian) {
        String s = "select r.id,r.name,r.count,r.gys from t_sheshi r where typeid=1 limit ?,?";
        List<Map> maps = JDBCUntil.selectMap(s + "", (pandian.getPage() - 1) * pandian.getLimit(), pandian.getLimit());
        List<TPandiandetail> list = new ArrayList<>();
        for (int i = 0; i < maps.size(); i++) {
            Map map = maps.get(i);
            TPandiandetail sheShi = new TPandiandetail((Integer) map.get("id"),(String)map.get("name"),(Integer)map.get("count"),(Integer)map.get("count"),"null",(String) map.get("gys"));
            list.add(sheShi);
        }
        return list;
    }
}

